#include<iostream>
using namespace std;

class A
{
public:
    virtual void func1()
    {
        cout << "A::func1()" << endl;
    }
    virtual void func2()
    {
        cout << "A::func2()" << endl;
    }
    virtual void func3()
    {
        cout << "A::func3()" << endl;
    }
};

class B
{
public:
    virtual void func1()
    {
        cout << "B::func1()" << endl;
    }
    virtual void func2xs()
    {
        cout << "B::func2()" << endl;
    }
};


class C :public A ,public  B
{
public:
    virtual void func1()
    {
        cout << "C::func1()" << endl;
    }
    /*virtual void func3()
    {
        cout << "C::func3()" << endl;
    }*/
    virtual void func4()
    {
        cout << "C::func4()" << endl;
    }
};

typedef void(*VF_PTR)(); //VF_PTR是函数指针,指向的函数返回值为void,无参

//打印任意类型对象的虚表
template<class T>
void PrintVFTable(T& t)
{
    VF_PTR* p_vf = static_cast<VF_PTR*>(*(void**)&t); //虚表指针,p_vf指向虚表(保存的是虚表的地址)
    cout << typeid(t).name() << "虚函数表的地址为:" << p_vf << endl;
    for (int i = 0; p_vf[i]; i++)
    {
        printf("vftable[%d] = 0x%p----->", i, p_vf[i]);
        p_vf[i]();
    }
}

void test()
{
    C c;

    VF_PTR* p_vf = static_cast<VF_PTR*>(*(void**)&c); 
    VF_PTR* p_vf2 = static_cast<VF_PTR*>( *(void**)((char*)&c + sizeof(A)));
    cout << typeid(c).name() << "虚函数表的地址为:" << p_vf << endl;
    for (int i = 0; p_vf[i]; i++)
    {
        printf("vftable[%d] = 0x%p----->", i, p_vf[i]);
        p_vf[i]();
    }
    cout << endl;
    for (int i = 0; p_vf2[i]; i++)
    {
        printf("vftable[%d] = 0x%p----->", i, p_vf[i]);
        p_vf[i]();
    }
}
int main()
{
    A a;
    B b;
    
   //PrintVFTable<A>(a);
    PrintVFTable<B>(b);
    cout << endl;
    /*PrintVFTable<C>(c);*/
    test();


    return 0;
}
